﻿using SmazSharp;
using SysHook;
using System;
using System.IO;
using System.Text;

namespace ClipboardSecure
{
    internal class RecordCenter : IDisposable
    {
        public RecordCenter()
        {
            this.enable = false;
        }
        private Stream RecordOutStream { get; set; }
        private BinaryWriter RecordOutStreamWriter { get; set; }
        private bool enable { get; set; }
        public void Dispose()
        {
            SetDisable();
        }
        internal bool Enabled => enable;
        internal void SetEnable()
        {
            if (enable) return;
            RecordOutStream = new FileStream(GlobalSettings.report_path, FileMode.OpenOrCreate);
            RecordOutStreamWriter = new BinaryWriter(RecordOutStream);
            enable = true;
        }
        internal void SetDisable()
        {
            if (!enable) return;
            enable = false;
            RecordOutStreamWriter.Close();
            RecordOutStream.Dispose();
        }
        internal void Record(string text)
        {
            if (!GlobalSettings.report) return;
            if (GlobalSettings.report_timestamp)
            {
                RecordOutStreamWriter.Write(DateTime.UtcNow.Ticks);
            }
            if (GlobalSettings.report_full)
            {
                var bytes = Encoding.UTF8.GetBytes(text);
                var buf = Smaz.Compress(bytes);
                RecordOutStreamWriter.Write(buf.Length);
                RecordOutStreamWriter.Write(buf);
                RecordOutStreamWriter.Flush();
            }
        }
    }
}
